Some general observations on the code:
-
if (ref($replacement) eq 'ARRAY' && length $replacement > 1) { ... }
length always operates on a string or stringized expression. A stringized reference looks something like "ARRAY(0x123abc)", so the expression length $replacement > 1 will always be true if $replacement is a reference.
Win8 Strawberry 5.8.9.5 (32) Fri 08/05/2022 17:16:08
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $arrayref = [ ]; # ref. to zero length array
print "$arrayref \n";
print "length non-zero \n" if length $arrayref > 1;
^Z
ARRAY(0x613824)
length non-zero
The number of elements of an array is found by evaluating the array in scalar context
(update: see Context and subsequent topics in perldata)
or, in newer Perls (5.12+), by the keys built-in evaluated in scalar context (check your documentation).
Win8 Strawberry 5.30.3.1 (64) Fri 08/05/2022 17:39:54
C:\@Work\Perl\monks
>perl
use strict;
use warnings;
my $arrayref = [ 9, 8, ]; # ref. to NON-zero length array
printf "number of elements of referenced array: %d \n", scalar @$
+arrayref;
printf "number of elements of referenced array: %d \n", 0 + @$
+arrayref;
printf "number of elements of referenced array: %d \n", scalar keys @$
+arrayref;
print "number of elements > 1 \n" if @$arrayref > 1;
print "number of elements > 1 \n" if keys @$arrayref > 1;
^Z
number of elements of referenced array: 2
number of elements of referenced array: 2
number of elements of referenced array: 2
number of elements > 1
number of elements > 1
(Update: Just to be clear, a proper re-statement of the condition expression would be
if (ref($replacement) eq 'ARRAY' && @$replacement > 1) { ... }
The > numeric comparison imposes scalar context.)
-
if ($choice eq 'y' || 'Y') { ... }
This condition evaluates as "if $choice is 'y' or if the string 'Y' is true", i.e., it's always true because 'Y' is always true. See What is true and false in Perl?
An effective case-insensitive conditional test would be
if (lc($choice) eq 'y') { ... }
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|