Section 1

Preview this deck

What does my $ar = [1..5]; mean?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (58)

Section 1

(50 cards)

What does my $ar = [1..5]; mean?

Front

It means $ar is an anonymous array reference to an array containing 1,2,3,4,5. To create an anonymous array reference, use square brackets [ ] instead of parentheses ( ).

Back

What is the dereference?

Front

Back

What is the "for of" equivalence in Perl?

Front

for my $array_element ($the_array){ say $array_element; } OR foreach my $array_element ($the_array){ say $array_element; } OR for (@the_array){ say "Your place in line is $_"; } //here $_ is used when no variable is declared

Back

What does 'my' do?

Front

A 'my' keyword declares the listed variables to be local (lexically) to the enclosing block, file, or eval. That variable would now be available to the scope of that file. If more than one variable is listed, the list must be placed in parentheses.

Back

What is @ARGV?

Front

the arguments to the command line of the program eg. $ perl examples/shift_argv.pl one two 'one' and 'two' are @ARGV

Back

What's the difference between a pragma and a module?

Front

Back

How do you map all elements in an array?

Front

my @mapped = map{$_ * 2} @number_arr;

Back

How do you sort or reverse sort an array?

Front

sort @arr; reverse sort @arr;

Back

What is the convention for naming the constructor in Perl?

Front

sub new

Back

How do you add a break statement?

Front

By typing "last;"

Back

How do you check if a key exists in a hash?

Front

exists $dogs{ChiChi};

Back

What does 'use vars qw($VERSION)' do?

Front

It creates a global variable named $VERSION. Note that use vars has been superseded by our. With perl5.6 you can use the 'our' operator instead

Back

How do you filter out values in array?

Front

By using grep, eg. my @large_numbers = grep{ $_ > 100 } @some_num_arr;

Back

How do you make an array out of some elements in another array?

Front

my @larger_arr = (1,2,3,4,5,6); my @smaller_arr = @larger_arr[3, 5]; so now @smaller_arr = (4, 6);

Back

What is a scalar?

Front

A data type that holds one variable, one value A dollar sign $ makes a variable a scalar

Back

How do you split a string?

Front

my $str_to_be_splitted = "Split,me,please"; my @splitted = split /,/ , $str_to_be_splitted; What you put between the // will be what the string will be splitted by

Back

How do you make an array of keys from a hash?

Front

my @arr_of_keys = keys %target_hash;

Back

How do you uppercase or lowercase all letters in a string?

Front

uc $string lc $string

Back

What is a pragma?

Front

A pragma is a module which influences some aspect of the compile time or run time behaviour of Perl, such as strict or warnings

Back

What are modules named?

Front

.pm instead of .pl

Back

How do you round a number?

Front

int(number_to_be_rounded);

Back

How do you delete a key value pair in a hash?

Front

delete $dogs{Lucky};

Back

What are class, object, method, and constructor called in Perl?

Front

A class is a package. An object is a reference that knows its class. A method is a subroutine. A subroutine called new() is used to construct the object

Back

How do you splice an array?

Front

splice @arr, 0, 3; 0 is the starting spliced index; 3 is the number of items you want to splice; You can't store the spliced values into a separate array

Back

How do you add a new key-value pair to a hash?

Front

$some_hash{newKey} = "new value";

Back

What are Misc::HTML and Misc::Utility?

Front

Back

How do you use pass in an array or a hash properly into a function?

Front

You need the \ thing in front of the array or hash when you pass them in as arguments eg. printTest(\@sample_Array); within the the function definition, you need to reference to the array with the $ sign eg. @$sample_Array

Back

What is IO::File module used for?

Front

For reading and writing files

Back

What does 'use' do?

Front

This function imports all the functions exported by MODULE, or only those referred to by LIST, into the name space of the current package Note that a use statement is evaluated at compile time. A require statement is evaluated at execution time.

Back

What does @{} mean?

Front

dereference

Back

What is the meaning of @_?

Front

@_ is the list of incoming parameters to a subroutine (ie. function). It's an array of parameters. Usually, you expand the parameters passed to a sub using the @_ variable: sub generateNames{ my ($a, $b, $c) = @_; ... } # call the test sub with the parameters generateNames('alice', 'bob', 'charlie');

Back

How do you switch values of 2 scalars?

Front

($first, $second) = ($second, $first);

Back

How do you join array elements>

Front

join(", ", $the_array)

Back

How can you get the count of elements in an array?

Front

Assign the array to a scalar eg. my @testArr = (1,2,3,4); my $count = @testArr; $count would print out 4 OR my $count = scalar @testArr;

Back

What is the difference between print and say?

Front

Say automatically adds a new line Print doesn't. To add a new in print, you need to add the new line character

Back

How do you reverse an array?

Front

reverse @arr;

Back

What is $_ ?

Front

It's a global variable that happens to be used by default by many operators. It is used when no variable is declared. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.

Back

Can you use single quote in Perl?

Front

Yes, but it would not recognize variable embedded in it. For that, you need double quote

Back

How do you generate a random number from between 1 to 10?

Front

rand 11;

Back

How do you make a variable retain its value after a function is called?

Front

by using state eg. state $kept_value = 0;

Back

How do you make a range of numbers or letters array?

Front

my @letters = ('a' .. 'z'); //this would make an array of letters from a to z my @numbers = (1 .. 10); //this would make an array of numbers of 1 to 10

Back

How do you set a default parameter in case no parameter was passed?

Front

sub say_hello { my ($name) = @_; $name ||= "Anonymous"; return "Hello $name"; }

Back

What is Perl reference used for?

Front

Back

What does shift do?

Front

If used in the main program, it will shift (remove and return) the first value from @ARGV, the argument list of your program. If used inside a subroutine, it will shift the first value from @_, the argument list of the sub. 'shift' accepts an array and extracts the first element (actually removing it from the array). If you don't provide any array, it uses @ARGV (the arguments to the command line of the program) to read the data. If you're inside a function, it uses @_, namely, the parameters of the function. In all cases, shift removes the element at list index 0 and returns it, shifting all remaining elements down one. Inside a sub, shift without an argument (bare shift) operates on the @_ list of subroutine parameters. Suppose I call mysub('me', '22') sub mysub { my $self = shift; # return $_[0], moves everything else down, $self = 'me', @_ = ( '22' ) my $arg1 = shift; # returns $_[0], moves everything down, $arg1 = '22', @_ = () } If you call a method on an object like this: $obj->my_method('one'); Then the my_method subroutine will actually be passed two arguments in @_. The first is the object that the method was invoked on (sometimes called the invocant) which in this example is $obj. Whenever you call the new() method, Perl automatically passes the class name (eg. Animals) as the first argument to the special array @_. The second argument will be the string 'one'. It is very common in object-oriented code to start a method definition like this: sub my_method { my $self = shift; } Which means $self will now contain the invocant (the class/object that invoked the function) and @_ will be left with all the remaining arguments.

Back

What does the 'sub' keyword do?

Front

Define a function. In some languages there is a distinction between functions and subroutines. In Perl there is only one thing. It is created with the sub keyword, and it always returns a value. Perl programmers often use the two words function and subroutine interchangeably.

Back

How do you add double quote to a string in Perl?

Front

"My name is \"$variable_name\"" OR a cleaner way of writing is qq{The book I'm reading is called "$book_title"}

Back

What is @INC?

Front

@INC is a built-in array perl provides. It contains a series of directories, the "search path" for perl when trying to load a module. When perl encounters a use statement, a require statement, or a do statement: use Module::Name; require Module::Name; do "Module/Name.pm"; perl will go over the directories listed in @INC and check if the appropriate file can be found. In our example, if we try to load Module::Name. Perl will check if there is a Module subdirectory in any of of the directories with a file called Name.pm in the directory. perl will load the first such file.

Back

How can you determine whether a Perl module named Foo is available for you to use?

Front

perl -e 'use Foo;'

Back

How do you dereference a hash?

Front

my %dogs = ( "Cookie"=>"fat", "Minnie"=>"shy", "Maggie"=>"overly affectionate", "Lily"=>"old and cute" ); To get the word "fat", we do $dogs{Cookie} Notice that %dogs is now $dogs

Back

Can you increment letter in Perl?

Front

Yes eg. my $a = "a"; my $b = ++$a; say $b; //this would print out "b"

Back

Section 2

(8 cards)

What are the prefix for dereferencing a scalar, array, and hash

Front

reference for all types - \ dereferencing scalar - $$ dereferencing array - @$ dereferencing hash - %$

Back

What do these lines mean? package Dog; sub new { my $class = shift; }

Front

new is a constructor my $class = shift; means setting the Dog package to be what $class references to.

Back

What does 'bless' do?

Front

Back

How do you make multiple variables out of an array?

Front

Eg. my @dog = ('Cookie', 'old', 'fat', 'vicious', 'cute'); my ($name, $age, $body_status, $toStrangers, $toUs) = @dog; So now, $name is 'Cookie', $age is 'old', and so on

Back

How do you find the length of an array in perl?

Front

Assign that array to a scalar variable eg. my @numArr = (1,2,3,4,5); my $numArrLength = @numArr; $numArrLength is 5;

Back

How do you set a default parameter if no argument was passed?

Front

sub say_hello { my ($name) = @_; $name ||= "Anonymous"; return "Hello, $name"; } say say_hello(); ==> Hello, Anonymous say say_hello('Darkness, my old friend'); ==> Hello, Darkness, my old friend

Back

How you use a ternary in Perl?

Front

my $ConfusedTorie = "Yes"; my $isConfused = $ConfusedTorie eq "Yes" ? "What is going on?" : "Crystal Clear";

Back

How do you make a string upper case?

Front

my $someStr = ''tester tester"; uc $someStr; ==> "TESTER TESTER"

Back