Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Length of String

by bigal_george (Acolyte)
on Dec 30, 2019 at 08:00 UTC ( [id://11110752]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Im new to perl I just dont get:
my $overwrite = length($function)>1; print STDERR "overwrite=$overwrite\n";
#if strlength==2 then prints 1, otherwise returns null, why not print 0? Thanks.....

Replies are listed 'Best First'.
Re: Length of String
by dsheroh (Monsignor) on Dec 30, 2019 at 10:22 UTC
    Point of order: It doesn't return null (undef in Perl terminology), it returns a special value which evaluates as an empty string when a string is expected (such as when printing) or as the number 0 when a number is expected1.

    If you want to print "0" instead of an empty string, you need to force it to be used as a number first:

    my $overwrite = (length($function)>1) + 0;
    or use the ternary operator (which also allows you to set values other than 1/0, if you want to):
    my $overwrite = length($function)>1 ? 1 : 0;
    Note, though, that this is only useful for making printed output look the way you expect. The original value returned by > will work perfectly well for flow control purposes (e.g., in an if condition) without needing to be converted to a plain 0 first.

     

    1 Yes, a normal empty string is also evaluated as 0 when a number is expected, but, if warnings are active, you'll get an "argument isn't numeric" warning when doing so. The "special" value returned by false boolean operators does not generate this warning.
      Great answers thanks. Thats a bit weird to me, that a variable can be either/or, but Ive got it now. Heres how its used in my code. Not sure if using chop is good practice, but it works..........
      my ($function, $sourcedir, $targetdir) = @ARGV; die ": missing operand. Usage: $0 <OPTION>... <SOURCE_DIRECTORY>... [T +ARGET_DIRECTORY]... OPTION all the FILE(s) and DIRECTORIES: -c = copy or -cy = copy overwrite -m = move or -my = move with overwrite -d = delete \n" unless (defined $function && defined $sourcedir); #@ARGV; #check validity input opendir(my $source_handle, $sourcedir) or die "Can't opendir $sourcedi +r: $!\n"; my $overwrite = 0 + (length $function>2) && $function =~ m/..y/ ; #strip last character ($overwrite now instantiated; no need for 'y') chop($function);

        You don't show how $overwrite is used later on, but I don't really see the need for the +0 here, since you've just got two boolean operations. But it doesn't hurt either.

        Not sure if using chop is good practice, but it works...

        chop just removes the last character from the string, so if you just want to remove the "y" from "-cy", it's perfectly fine. However, in this code, you seem to be using it unconditionally, so it would also turn "-c" into "-"...

        (length $function>2) && $function =~ m/..y/

        Note that this will also be true for strings like "silly putty". You might want to make your regex more specific by anchnoring it and limiting it to expected characters, something like /^-[cm]y$/, then you don't even need the length check. You could even combine that with removing the y like so: s/^-[cm]\Ky$// (where \K basically means "keep").

        By the way, I suggest having a look at Getopt::Long, which can take over a lot of the option processing for you, I showed an example that includes "usage" information here.

Re: Length of String
by haukex (Archbishop) on Dec 30, 2019 at 08:58 UTC

    What you are storing in $overwrite is the return value of the > operator, which will be Perl's version of "boolean" values - see Truth and Falsehood. If you wanted to store other values, you could use the Conditional Operator: my $overwrite = length($function) ? "true" : "false"; (those are two strings that are both considered "true" in Perl).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11110752]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-20 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found