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

I have modified the code here: http://code.google.com/p/serial-logger/source/browse/trunk/logger.pl and added: use warnings It now complains about $sub I have searched for some information but haven't come up with much. Since this is a very short program - very close to the same size as the logger code referenced I see little need to knowing where the error occured as there is only one statement like this so it seems like I could just drop that part of the error output. Also wondering what "smdr" in the error message refers to.

Replies are listed 'Best First'.
Re: use of $sub
by toolic (Bishop) on Oct 19, 2012 at 14:08 UTC
    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.
      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 "$".

        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'

        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.

        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.

        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.
Re: use of $sub
by tobyink (Canon) on Oct 19, 2012 at 14:28 UTC

    SMDR appears to be a log file format used by various communications hardware.

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