Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: use of $sub

by toolic (Chancellor)
on Oct 19, 2012 at 14:08 UTC ( #999933=note: print w/ replies, xml ) Need Help??


in reply to use of $sub

The string $SUB only shows up in that code once. You probably get a warning message like:

Name "main::SUB" used only once: possible typo
Generally, variables are expected to be assigned a value before they are used. Use diagnostics to get more information.
Also wondering what "smdr" in the error message refers to.
What error message? You should post the exact error message you are receiving, as well as at least a snippet of the relevant code.


Comment on Re: use of $sub
Select or Download Code
Re^2: use of $sub
by JimLS (Novice) on Oct 19, 2012 at 14:20 UTC
    smdr appears in the text of the error message of the code I linked to in the same line of the code that has $sub. I think it may have simply been something to do with the original use of the code. It wasn't part of a system error output. Yes, the error is 'used only once' as you stated. I think my real question is how do I use $sub with 'use warnings' and not get the warning? Also, the links I found on the use of sub were "sub" rather than "$sub" and I would like to understand the addition of "$".
      Go ahead and add use diagnostics; to see
      $ perl -Mdiagnostics -e " $foo = 1; " Name "main::foo" used only once: possible typo at -e line 1 (#1) (W once) Typographical errors often show up as unique variable nam +es. If you had a good reason for having a unique name, then just menti +on it again somehow to suppress the message. The our declaration is provided for this purpose. NOTE: This warning detects symbols that have been used only once s +o $c, @c, %c, *c, &c, sub c{}, c(), and c (the filehandle or format) are con +sidered the same; if a program uses $c only once but also uses any of the +others it will not trigger this warning.

      warnings are useful, but adding strict is better. Read this if you want to cut your development time in half! explains how/why to use them.

      I would like to understand the addition of "$"

      Not, "$" but "$!". The exclamation mark is not just for decoration. See perlvar.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Not, "$" but "$!".

        No, I don't mean "$!". I looked up "$!" and understood that. I mean the addition of "$" to "SUB" resulting in "$SUB".
      smdr is inside a quoted string of text. You have to ask the original author what he meant by smdr.

      not get the warning?
      Like I said, you need to assign a value to $SUB, or just delete the variable if you don't need it.

      JimLS:

      I expect that just adding something like this:

      sub PREVENT_USELESS_WARNING { my $t = $sub }

      or similar shenanigans would do fine.

      ...roboticus

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999933]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-09-02 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls