Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Length of String

by bigal_george (Acolyte)
on Dec 30, 2019 at 17:25 UTC ( [id://11110775]=note: print w/replies, xml ) Need Help??


in reply to Re: Length of String
in thread Length of String

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);

Replies are listed 'Best First'.
Re^3: Length of String
by haukex (Archbishop) on Dec 30, 2019 at 18:54 UTC

    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.

      Ohhps: $overwrite>0 && chop($function);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found