in reply to Pls explain this syntax

when a variable is declared in the main block only, why do we neeed to make it local ?

If a variable is declared local in the main block, does it mean that if a subroutine is declared then this variable can not be used inside subroutines as its declared outside the subroutine as local ?

It depends on the ordering, since the subroutines exist in the file's lexical scope (what you call "the main block").

use strict; my $x; sub foo { $x = 'foo'; # Ok } foo(); print($x); # foo

-vs-

use strict; sub foo { $x = 'foo'; # Refers to package var $main::x. # It's also a compilation error. } my $x; foo(); print($x); # foo

my $dirname = dirname $old_name ; # what does this syntax mean ?

Call sub dirname with argument $old_name. Declare $dirname and assign the result of the call to dirname to it.

s/^/not/, # does it mean that not will be added at the start of the string ?

Yes. Why didn't you try it?

/^/ in a regexp patterm means "the start of the string" (or "the start of the line" if the "m" modifier is used). So s/^/not/ means "replace the start of the string with 'not'" or in better English, "prepend 'not' to the string". It could have been written

$basename = 'not' . $basename;

(my $basename = $name =~ s#./##; # how come there is only one forward slash ?

Why not?

Though that's buggy for two reasons. There's a missing closing paren, and it should be s#.*/##s ("replace everything up to the last slash with nothing").

Replies are listed 'Best First'.
Re^2: Pls explain this syntax
by manish.rathi (Acolyte) on Feb 23, 2009 at 03:28 UTC

    I am still having confusion about my last question. We can use any delimiter / or #. s#^https://#http://# ; in this syntax # is used instead of /

    s#./##s; in this syntax, it looks that delimiter is #, but can we use different separator between two strings than the delimitor. It looks the separator is "/".

    Pls explain

      The character (space excepted) that immediately follows the "s" is the delimiter.

      It looks the separator is "/".

      No, it's "#", since that's the character that follows the "s". "./" is the pattern, "" is the replacement.