Discover how to efficiently handle dates, times, and strings in Perl with modules like Time::Piece and DateTime. Learn advanced techniques for string manipulation, parsing, and tokenization.
Using Time::Piece and DateTime (10 Questions)
What is the main function of the Time::Piece module in Perl? A) To parse and format date/time strings B) To perform calculations with dates and times C) To create and manipulate time objects D) To connect to time servers
How can you get the current time using Time::Piece? A) Time::Piece->new() B) localtime() C) Time::Piece->now() D) Time::Piece->current_time()
Which of the following methods is used to add days to a Time::Piece object? A) add_days() B) add() C) + operator D) Time::Piece->add_days()
Which module is commonly used in Perl for working with date and time objects with advanced features? A) Date::Time B) DateTime C) Time::Local D) DateTime::Local
What does DateTime->now() return? A) The current system date and time B) The current time in UTC C) The current date D) A new DateTime object with the local time
Which method in DateTime can be used to extract the year from a DateTime object? A) get_year() B) year() C) date() D) year_value()
What does DateTime::Format::Strptime do in Perl? A) Converts a DateTime object to a string B) Parses a date/time string into a DateTime object C) Formats a DateTime object into a specific format D) Validates date/time string formats
Which of these is true about the Time::Piece object? A) It is immutable once created B) It cannot be modified C) It is designed for date comparison only D) It allows easy manipulation of date and time
How can you compare two DateTime objects in Perl? A) Using the compare() method B) Using comparison operators (==, <, >) C) Using the equal() method D) Using the equals() method
Which method is used to format a DateTime object into a human-readable string? A) strftime() B) format() C) to_string() D) as_string()
Manipulating Strings Efficiently (10 Questions)
Which function is used in Perl to concatenate strings? A) concatenate() B) concat() C) . (dot operator) D) join()
How can you remove leading and trailing whitespace from a string in Perl? A) chomp() B) trim() C) strip() D) s/^\s+|\s+$//g
Which of the following Perl functions is used to split a string into an array based on a delimiter? A) split() B) explode() C) tokenize() D) parse()
How can you check if a string contains a substring in Perl? A) index() B) contains() C) match() D) find()
Which function is used to replace a part of a string with another string in Perl? A) replace() B) substitute() C) s/// (substitution operator) D) modify()
Which operator is used in Perl to repeat a string multiple times? A) * B) x C) repeat() D) dup()
Which function is used to find the length of a string in Perl? A) len() B) size() C) length() D) str_len()
How can you convert a string to lowercase in Perl? A) lower() B) lc() C) tolower() D) to_lower()
Which function would you use to match a regular expression against a string in Perl? A) match() B) regex() C) m// (match operator) D) find_regex()
How can you trim spaces from the beginning of a string in Perl? A) trim_start() B) ltrim() C) lstrip() D) s/^\s+//
Advanced String Parsing and Tokenization (10 Questions)
What does the split() function do when used with a regular expression in Perl? A) It splits the string at the first occurrence of the regular expression B) It splits the string into an array based on the regular expression C) It returns the entire string as a single element D) It counts the occurrences of the regular expression
Which of these is a valid delimiter to split a string by using split() in Perl? A) A character B) A regular expression C) A space D) All of the above
How can you tokenize a string using a space delimiter in Perl? A) split(" ", $string) B) split(' ', $string) C) tokenize($string) D) parse($string, " ")
What does the join() function do in Perl? A) It splits a string into multiple parts B) It combines multiple strings into one with a delimiter C) It checks if a string contains another string D) It finds the length of a string
Which function is used to parse CSV (Comma Separated Values) data in Perl? A) parse_csv() B) CSV::Parser C) Text::CSV D) split_csv()
How can you parse a string into tokens separated by commas? A) split(',', $string) B) tokenize(',', $string) C) split(";", $string) D) parse(',', $string)
What does the qr// operator in Perl do? A) Compiles a regular expression for later use B) Translates a string into a regular expression C) Matches a regular expression against a string D) Joins multiple regular expressions
How can you extract a substring from a string using regular expressions in Perl? A) Using substr() B) Using split() C) Using a capture group in a regular expression D) Using match()
Which function would you use to extract a match from a string in Perl? A) match() B) extract() C) m// (match operator) D) find()
What does the $_ variable represent in Perl when working with regular expressions? A) The current string being processed B) The last matched string C) The current position in the string D) The last token
Answers Table
QNo
Answer (Option with Text)
1
C) To create and manipulate time objects
2
C) Time::Piece->now()
3
C) + operator
4
B) DateTime
5
A) The current system date and time
6
B) year()
7
B) Parses a date/time string into a DateTime object
8
D) It allows easy manipulation of date and time
9
B) Using comparison operators (==, <, >)
10
A) strftime()
11
C) . (dot operator)
12
D) `s/^\s+
13
A) split()
14
A) index()
15
C) s/// (substitution operator)
16
B) x
17
C) length()
18
B) lc()
19
C) m// (match operator)
20
D) s/^\s+//
21
B) It splits the string into an array based on the regular expression
22
D) All of the above
23
B) split(' ', $string)
24
B) It combines multiple strings into one with a delimiter