1 2 >   Sort: Rank

How do you give functions private variables that retain their values between calls?
How do you give functions private variables that retain their values between calls? Create a scope surrounding that sub that contains lexicals. Only lexical variables are truly private, and they will persist even when their block exits if something still cares about them. Thus: { my $i = 0; sub next...
2013-09-25, 1441👍, 0💬

How many ways can we express string in Perl?
How many ways can we express string in Perl? Many. For example 'this is a string' can be expressed in: "this is a string" qq/this is a string like double-quoted string/ qq^this is a string like double-quoted string^ q/this is a string/ q&this is a string& q(this is a string)
2013-09-25, 1495👍, 0💬

Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical? They are not identical. There are several differences between using single quotes and double quotes for strings. 1. The double-quoted string will perform variable interpolat...
2013-09-24, 1443👍, 0💬

How do I print the entire contents of an array with Perl?
How do I print the entire contents of an array with Perl? To answer this question, we first need a sample array. Let's assume that you have an array that contains the name of baseball teams, like this: @teams = ('cubs', 'reds', 'yankees', 'dodgers'); If you just want to print the array with the arra...
2013-09-24, 1495👍, 0💬

How do you match one letter in the current locale?
How do you match one letter in the current locale? /[^\W_\d]/ We don't have full POSIX regexps, so you can't get at the isalpha() <ctype.h> macro save indirectly. You ask for one byte which is neither a non-alphanumunder, nor an under, nor a numeric. That leaves just the alphas, which is what ...
2013-09-23, 1427👍, 0💬

Assume that $ref refers to a scalar, an array, a hash or to some nested data structure. Explain the following statements:
Assume that $ref refers to a scalar, an array, a hash or to some nested data structure. Explain the following statements: $$ref; # returns a scalar $$ref[0]; # returns the first element of that array $ref- > [0]; # returns the first element of that array @$ref; # returns the contents of that arra...
2013-09-23, 1465👍, 0💬

What happens to objects lost in "unreachable" memory..... ?
What happens to objects lost in "unreachable" memory..... ? What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ? Their destructors are called when that interpreter thread shuts down. When the interpreter ex...
2013-09-20, 1459👍, 0💬

When would `local $_' in a function ruin your day?
When would `local $_' in a function ruin your day? When your caller was in the middle for a while(m//g) loop The /g state on a global variable is not protected by running local on it. That'll teach you to stop using locals. Too bad $_ can't be the target of a my() -- yet.
2013-09-20, 1442👍, 0💬

How do I read command-line arguments with Perl?
How do I read command-line arguments with Perl? With Perl, command-line arguments are stored in the array named @ARGV. $ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc. $#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the c...
2013-09-19, 1446👍, 0💬

How to concatenate strings with Perl?
How to concatenate strings with Perl? Method #1 - using Perl's dot operator: $name = 'checkbook'; $filename = "/tmp/" . $name . ".tmp"; Method #2 - using Perl's join function $name = "checkbook"; $filename = join "", "/tmp/", $name, ".tmp"; Method #3 - usual way of concatenating strings $filename = ...
2013-09-19, 1425👍, 0💬

What is the easiest way to download the contents of a URL with Perl?
What is the easiest way to download the contents of a URL with Perl? Once you have the libwww-perl library, LWP.pm installed, the code is this: #!/usr/bin/perl use LWP::Simple; $url = get 'http://www.websitename.com/';
2013-09-18, 1342👍, 0💬

How do I replace every <TAB> character in a file with a comma?
How do I replace every <TAB> character in a file with a comma? perl -pi.bak -e 's/\t/,/g' myfile.txt
2013-09-18, 1465👍, 0💬

How do I do < fill-in-the-blank > for each element in an array?
How do I do < fill-in-the-blank > for each element in an array? #!/usr/bin/perl -w @homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth'); foreach (@homeRunHitters) { print "$_ hit a lot of home runs in one year\n"; }
2013-09-16, 1407👍, 0💬

If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}?
If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}? The second is disallowed under `use strict "refs"'. Dereferencing a string with *{"STR"} is disallowed under the refs stricture, although *{STR} would not be. This is similar in spirit to the way ${"...
2013-09-16, 1468👍, 0💬

What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?
What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash? 5 length() is a built-in prototyped as sub length($), and a scalar prototype silently changes aggregates into radically different forms. The scalar sense of a hash is false (0) if it's empty, otherwise it's...
2013-09-13, 1390👍, 0💬

How to dereference a reference?
How to dereference a reference? There are a number of ways to dereference a reference. Using two dollar signs to dereference a scalar. $original = $$strref; Using @ sign to dereference an array. @list = @$arrayref; Similar for hashes.
2013-09-13, 1434👍, 0💬

Does Perl have reference type?
Does Perl have reference type? Yes. Perl can make a scalar or hash type reference by using backslash operator. For example $str = "here we go"; # a scalar variable $strref = \$str; # a reference to a scalar @array = (1..10); # an array $arrayref = \@array; # a reference to an array Note that the ref...
2013-09-12, 1340👍, 0💬

What's the difference between /^Foo/s and /^Foo/?
What's the difference between /^Foo/s and /^Foo/? The second would match Foo other than at the start of the record if $* were set. The deprecated $* flag does double duty, filling the roles of both /s and /m. By using /s, you suppress any settings of that spooky variable, and force your carets and d...
2013-09-12, 1328👍, 0💬

What value is returned by a lone `return;' statement?
What value is returned by a lone `return;' statement? The undefined value in scalar context, and the empty list value () in list context. This way functions that wish to return failure can just use a simple return without worrying about the context in which they were called.
2013-09-11, 1381👍, 0💬

How do you find the length of an array?
How do you find the length of an array? $@array
2013-09-11, 1322👍, 0💬

How do I sort a hash by the hash value?
How do I sort a hash by the hash value? Here's a program that prints the contents of the grades hash, sorted numerically by the hash value: #!/usr/bin/perl -w # Help sort a hash by the hash 'value', not the 'key'. to highest). sub hashValueAscendingNum { $grades{$a} <=> $grades{$b}; } # Help s...
2013-09-10, 1392👍, 0💬

What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)
What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.) $cur->new()->{LINK} The indirect object syntax only has a single token lookahead. That means if new() is a method, it only grabs the very next token, not the entire following expression. This is why...
2013-09-09, 1336👍, 0💬

What does read() return at end of file?
What does read() return at end of file? 0 A defined (but false) 0 value is the proper indication of the end of file for read() and sysread().
2013-09-09, 1325👍, 0💬

Why does Perl not have overloaded functions?
Why does Perl not have overloaded functions? Because you can inspect the argument count, return context, and object types all by yourself. In Perl, the number of arguments is trivially available to a function via the scalar sense of @_, the return context via wantarray(), and the types of the argume...
2013-09-06, 1345👍, 0💬

1 2 >   Sort: Rank