Yisha sharma has asked for the wisdom of the Perl Monks concerning the following question:

Please help me to understand what this statement coresponds to

 $tmp=s/\*/\$lvl=/g

Regards Madhvi

Replies are listed 'Best First'.
Re: perl statement
by roboticus (Chancellor) on Nov 20, 2014 at 04:49 UTC

    Yisha sharma:

    Generally if you have a question like that, you look up the appropriate operator, statement or whatever in the docs. In this case, you could read perldoc perlop to find the documentation for the s/// operator (it's in the "quote-like operator" section), or you could just experiment with it a little and find out, like:

    $ cat tu.pl while (<DATA>) { #$tmp = s/\*/\$lvl=/g; # LTS! $tmp = s{\*}{\$lvl=}g; # ?better? print "$_ : $tmp\n"; } __DATA__ now is *the* time! just how many asterisks? ***** $ perl tu.pl now is $lvl=the$lvl= time! : 2 just how many asterisks? $lvl=$lvl=$lvl=$lvl=$lvl= : 5 :

    Update: I agree that I coulda done better with this node, davido's is much better. I sometimes forget how daunting the perl docs can be when starting. (Even now, I sometimes have a little time figuring out which perldoc to read for some topics.) I also changed the code to use an alternate form of the s/// operator to avoid Leaning Toothpick Syndrome (LTS).

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Generally if you have a question like that, you look up the appropriate operator, statement or whatever in the docs. Of course, I agree in general, but in this specific case, I can understand that the OP, if she or he does not know much of Perl, would not even know where to look. Also, these backslashes don't make it easy for a beginner.

        But roboticus does go on to constructively address the specific case: " In this case, you could ..." OTOH, davido's response below is much more detailed and, IMHO, preferable. In any event, ++ to both from me.

        And yes, those backslashes are rather eye- and brain-bezoggling; severe LTS.

Re: perl statement
by davido (Cardinal) on Nov 20, 2014 at 07:57 UTC

    On the right hand side of the = operator you have a substitution operator. Here's how the operator looks: s/PATTERN/REPLACEMENT/FLAGS. So the pattern is \*, which really means the literal * character. The replacement is the literal text "$lvl=". As often as '*' appears in the target string, it will be replaced with '$lvl='.

    There is no binding operator, so this regular expression is matching and substituting against the contents of $_, the implicit "it" or "topic" variable. In other words, in the absence of an explicit target, the regexp operators bind to $_. (Or was it just a typo in your code that there is no =~ operator?)

    The return value of a s///g or m//g operator in scalar context will be a true value if there was at least one match, and a false value otherwise. The true value that is returned in scalar context is defined to be the count of how many times the operator matched. The return value will be assigned to $tmp.


    Dave