1 2 >   Sort: Rank

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...
2023-09-13, 2816👍, 1💬

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, 2152👍, 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, 2228👍, 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, 2255👍, 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 ...
2013-09-23, 2170👍, 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 ...
2013-09-23, 2202👍, 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 interp...
2013-09-20, 2195👍, 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, 2144👍, 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, 2175👍, 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, 2164👍, 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, 2111👍, 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, 2791👍, 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, 2179👍, 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, 2294👍, 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, 2031👍, 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, 2131👍, 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, 2046👍, 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, 2078👍, 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, 2108👍, 0💬

How do you find the length of an array?
How do you find the length of an array? $@array
2013-09-11, 2093👍, 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}; } ...
2013-09-10, 2312👍, 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()->{LI NK}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. Thi...
2013-09-09, 2111👍, 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, 1966👍, 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, 2020👍, 0💬

1 2 >   Sort: Rank