Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Seekers of Perl Wisdom

( [id://479]=superdoc: print w/replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask.

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

Post a new question!

User Questions
Puzzled by value of $overload::ops{binary}
2 direct replies — Read more / Contribute
by syphilis
on Jun 24, 2024 at 08:48
    Hi all,

    On Windows (perl 5.40.0 and 5.38.0 at least):
    >perl -Moverload -le "print $overload::ops{binary};" & &= | |= ^ ^= &. &.= |. |.= ^. ^.=
    I am familiar with the first 6 operators, but the last 6 operators (ie the ones containing a ".") are not something I'm familiar with.
    For example, what would one expect $obj1 &. $obj2 to do ?
    Or is it just something that I can use to do whatever I want ?

    Are there examples somewhere (maybe in some CPAN module) of subs called by the overloading of those non-existent operators ?
    Is there some documentation about them ?

    It's odd that I haven't spotted them before - but tonight is the first time I've ever laid eyes on them.

Control warnings for each module by creating new warning categories
2 direct replies — Read more / Contribute
by bliako
on Jun 24, 2024 at 07:57

    For a recent post (Solve IO::Pty warning issues from Net::OpenSSH) I tried to create a new category for warnings for a module and then disable it but it failed (perl 5.34.0) when I used it as *I expected*:

    { package XXX; use strict; # register a new warnings category with this package name use warnings::register; sub expected { warn "ajajaj"; } sub correct { if( warnings::enabled() ){ warn "ajajaj"; } else { print "not warning!\n" } } 1; } # it's already loaded... #use XXX; no warnings('XXX'); # this says 'no' (as expected) print "enabled: ".(warnings::enabled('XXX')?"yes":"no")."\n"; XXX::expected(); # this still warns XXX::correct(); # this does not warn

    Firstly, correct me if I am doing something wrong.

    Secondly, am I expecting too much to just register a category within a module and then anyone from outside it, to enable or disable it? Why the check to warnings::enabled() in each and every warn() statement of that module. That makes refactoring to using warning categories very difficult.

try/finally syntax
4 direct replies — Read more / Contribute
by Superfox il Volpone
on Jun 23, 2024 at 08:58
    I'm on perl v5.34.1, with the "try" feature enabled. I still cannot figure out the correct syntax for:
    my $current_directory = getcwd(); try { chdir($working_directory); } finally { chdir($current_directory); };
    This fails with the error:
    syntax error at / line 56, near "} finally " Execution of / aborted due to compilation errors.
Solve IO::Pty warning issues from Net::OpenSSH
2 direct replies — Read more / Contribute
by dkhosla1
on Jun 22, 2024 at 17:30

    I am using Net::OpenSSH, and if providing a password to a connection, it uses the IO:Pty package and gives a warning.

    The connection is successful. The issue seems to be with grantpt()

    WARN: IO::Pty::new:24: IO::Tty::pty_allocate(nonfatal): grantpt(): Operation not permitted

    With debug it shows 'trying grantpt() ...' before this message.

    1) any suggestions on how to have grantpt() be successful or not be attempted?

    2) Or suppress that specific message or worst case suppress all warning messages from IO::Pty (It was suggested that I use '{ no warning; .. } but I don't want to suppress warnings from Net::OpenSSH itself)

SOLVED:use -d -t -s as script args
1 direct reply — Read more / Contribute
by vincentaxhe
on Jun 21, 2024 at 11:46
    I have script with code like that
    my %options; getopts("r:", \%options); ... $regex = qr($options{r}); foreach (@files){ my $filepath = $_->[0]; if ($filepath =~ $regex){ push @matched, $filepath; } }
    I run -r 'python' filelist to filter filepath containing 'python' but I also want to run like -e '-d' listfile to get files which are dirs or -e '-s > 100' filelist and more such as -t -M.
    my %options; getopts("e:", \%options); ... $wanted = qr($options{e}); sub getwanted(&@){ my ($wanted, $file) return 1 if $wanted->($file) ... } foreach (@files){ $filepath = $_->[0]; if (getwanted $wanted $filepath){ push @matched, $filepath; } }
    but the subroutine is not good, ask some advice
Can I get the actual error for DBI->execute() ?
4 direct replies — Read more / Contribute
by SergioQ
on Jun 20, 2024 at 21:43

    I have the function below that I use in perl. I build the command elsewhere as it could be a SELECT, INSERT, et al.

    In one instance I am using the function to insert new records into my MySQL table. And generally it works. But occasionally it will fail on an insert, and I was hoping I could find out what the actual reason an insert failed. To be clear I am inserting over a thousand records into this table, and most work.

    I even had my perl script print out the actual command that returns a nil on the result, and then I copy and paste that exact command into MySQL's terminal where it works. So even more reason to get the exact reason it failed.

    I should add that the table has no index, and no unique fields.

    #from elsewhere in my code ... $db=DBI->connect('DBI:mysql:invoices',[username],[password], { RaiseEr +ror => 0, AutoCommit => 1 }); #$rawDB is set to the table name my $insertCommand = "INSERT INTO $rawDB (sheet_no, sheet_name, item, c +oll, roww, category) VALUES ('$currentSheet', '$sheetname', '$cellData', '$curcol', '$curro +w', '');"; my $result = insertItem($insertCommand); #.... sub insertItem { (my $insertCom) = @_; $dbHandle=$db->prepare($insertCom); my $result = $dbHandle->execute(); return $result; }
Undefined address for Socket::pack_sockaddr_in
1 direct reply — Read more / Contribute
by WithABeard
on Jun 20, 2024 at 07:40

    I am using Firefox::Marionette, and it works great (normally), but is now giving this error:

    % perl -MFirefox::Marionette -e 'my $f=Firefox::Marionette->new()' Undefined address for Socket::pack_sockaddr_in at /Users/<user>/perl5/ +lib/perl5/Firefox/ line 7134.

    The line in question is this one:

    $sock_addr = Socket::pack_sockaddr_in( $port, Socket::inet_aton($host) );

    So I'm guessing that Socket::inet_aton($host) is the culprit tells us that:

    ...If passed a hostname that cannot be resolved, returns undef.

    From this I'll assume that the hostname cannot be resolved, but we're not even opening a url yet...

    Testing Socket yields predictable results:

    % perl -MSocket -e'defined (my $a=inet_aton("garbage")) or die"dead"; +print unpack "V", $a;' dead at -e line 1. % perl -MSocket -e'defined (my $a=inet_aton("")) or die"dead" +; print unpack "V", $a;' 16777343

    It seems to do its job when passed something sensible

    I usually work from home, but today I went in to the office, and got the error.
    Could this be caused by a change in the network?
    I'll know later today if it works at home.

    System details:

    MacOS 14.5
    Apple M2
    perl ver. 5.34.2

    I tried flushing cache with sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder, but this made no difference

    I also tried changing the perl version (using plenv), but then I have to install Firefox::Marionette for the new version, and surprise-surprise; installation fails with the same error :p

    Is this the right way to flush DNS on MacOS 14.5?

    How can I find out what is actually passed to inet_aton when it fails?

    What else can I do to debug this?

Data::Dumper print arrays of scalars on one line
4 direct replies — Read more / Contribute
by Danny
on Jun 19, 2024 at 23:06
    Is there a Data::Dumper option to print arrays of scalars on a single line, but everything else with indentation? For example, like:
    $VAR1 = { 'hk1' => 5, 'hk2' => [ [1, 2, 3], [4, 5, 8] ], etc ...
DBI->connect fails
4 direct replies — Read more / Contribute
by Milti
on Jun 19, 2024 at 15:13

    This error returns when Perl script attempts to connect to a known mysql database"DBI connect('membersdb','memeber',...) failed: Access denied for user 'memeber'@'localhost' (using password: YES) at". Why is this happening when 'ALL PRIVILEGES' on the database have been granted to 'member'@'localhost'. The connection can only be made if I use "root' 'root password'. Here is the statement GRANT ALL PRIVILEGES ON `membersdb`.* TO 'member'@'localhost'. What am I missing?

Regular Expression Assistance
3 direct replies — Read more / Contribute
by g_speran
on Jun 19, 2024 at 12:41

    Wondering if someone can put eyes on this and tell me what i am doing incorrectly. I've tried several option but make it worse

    Hoping someone can quickly spot my error.

    i need to get rid of the doublequotes after 'value=' and before '/>'

    Thanks in advance

    my $content = '<entry key="maxcompdatastripe" value="41943040" />'; my $maxreplacement_value = ' "20917520" merge="keep" '; $content =~ s/(<entry key="maxcompdatastripe" value=")[^"]*("\s*\/>)/$ +1$maxreplacement_value$2/g; print "Content: $content\n"; Expected results: <entry key="maxcompdatastripe" value="20917520" merge="keep" /> Actual Results: Content: <entry key="maxcompdatastripe" value=" "20917520" merge="keep +" " />

Add your question
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-06-24 17:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.