This is my 2nd script since I started learning/reading the new perl book "Programming Perl" I think I should ask what my coding style is and also how to fix the bug. Can I leave out OO code for a while or should I look at that first or are there any other problems with how I am approaching it.
its a project euler problem:
#!/usr/bin/perl
use v5.14;
my $result = 0;
sub fib
{
my @frame = @_;
@frame[0] = @frame[0] + @frame[1];
@frame[1] = @frame[2];
@frame[3] = @frame[2];
count($frame[0]);
return fib(@frame) unless($frame[0] >= 4000000);
}
sub count
{
my $increment = @_;
if(($increment % 2) == 0)
{
$result = $result + $increment;
}
}
fib(1,2,3);
print $result;
|
After a lot of tweaking and experimentation I got code that will display for me the functions that are defined in a bash shell on my system. I first wrote the code 13 years ago. I broke this code out of a much larger modulino that I started back then and never got entirely working.
This scriptlet is working in CygPerl but not in Perl on Linux.
#!/usr/bin/env perl
# Last modified: Wed Jul 16 2025 12:52:52 PM -04:00 [EDT]
# Bash-functions.pl
use strict;
use v5.18;
use utf8;
use warnings;
local $ENV{'PERL5SHELL'} = q=/usr/bin/bash=;
local $ENV{'SHELL'} = q=/usr/bin/bash=;
my $spawnFH;
my $elist;
my $cPid = open( $spawnFH, q[-|], qq[$ENV{'PERL5SHELL'} -login command
+ builtin declare -p -F] )
or die "Bad open from pipe: $!";
if ( $cPid ) {
$elist = join qq[], grep {/^declare -f/} <$spawnFH>;
close $spawnFH or warn qq/Bad close on process $cPid: $!|$?/;
}
print qq[Results from $cPid:\n], $elist, qq[\n];
__END__
Results on CygPerl, Bash 5.2.21
$ perl Bash-functions.pl
Results from 3716:
declare -f SBP
declare -f exp
declare -f gawklibpath_append
declare -f gawklibpath_default
declare -f gawklibpath_prepend
declare -f gawkpath_append
declare -f gawkpath_default
declare -f gawkpath_prepend
declare -f profile_d
Results on Gnu/Linux, Bash 5.2.15
$ perl Bash-functions.pl
/usr/bin/bash: command: No such file or directory
Bad close on process 12284: |32512 at Downloads/Bash-functions.pl line
+ 17.
Results from 12284:
Apparently bash behaves differently, when invoked this way on Linux. If I use the bash invocation alone (on Linux): command builtin declare -p -F, I see a nice list of functions. Also, I am puzzled wrt why I need to start the command with $PERL5SHELL. I would have thought, based on documentation I read, that seeing shell metacharacters in the open string would have caused perl to automatically invoke a shell.
Jul 16, 2025 at 17:46 UTC
A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen —> I.G.Y.
(Slightly modified for inclusiveness)
|
I have a problem parsing some json message
Here is the response I get from the request, I outputted few because the file is large
{
"get": "fixtures",
"parameters": {
"live": "all"
},
"errors": [],
"results": 43,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"fixture": {
"id": 1328845,
"referee": null,
"timezone": "UTC",
"date": "2025-07-12T12:30:00+00:00",
"timestamp": 1752323400,
"periods": {
"first": 1752323400,
"second": null
},
"venue": {
"id": 21763,
"name": "Skreia ipl kunstgress",
"city": "Skreia"
},
"status": {
"long": "First Half",
"short": "1H",
"elapsed": 25,
"extra": null
}
},
"league": {
"id": 774,
"name": "3. Division - Girone 1",
"country": "Norway",
"logo": "https://media.api-sports.io/football/leagues/774.png"
+,
"flag": "https://media.api-sports.io/flags/no.svg",
"season": 2025,
"round": "Group 1 - 14",
"standings": true
},
"teams": {
"home": {
"id": 23420,
"name": "Sortland",
"logo": "https://media.api-sports.io/football/teams/23420.pn
+g",
"winner": null
},
"away": {
"id": 6974,
"name": "Bćrum",
"logo": "https://media.api-sports.io/football/teams/6974.png
+",
"winner": null
}
},
"goals": {
"home": 0,
"away": 0
},
"score": {
"halftime": {
"home": 0,
"away": 0
},
"fulltime": {
"home": null,
"away": null
},
"extratime": {
"home": null,
"away": null
},
"penalty": {
"home": null,
"away": null
}
},
"events": []
},
{
"fixture": {
"id": 1394987,
"referee": null,
"timezone": "UTC",
"date": "2025-07-12T12:00:00+00:00",
"timestamp": 1752321600,
"periods": {
"first": 1752321600,
"second": null
},
"venue": {
"id": 21680,
"name": "Estádio Deputado Galdino Leite",
"city": "Salvador, Bahia"
},
"status": {
"long": "Halftime",
"short": "HT",
"elapsed": 45,
"extra": null
}
},
"league": {
"id": 1073,
"name": "Baiano U20",
"country": "Brazil",
"logo": "https://media.api-sports.io/football/leagues/1073.png
+",
"flag": "https://media.api-sports.io/flags/br.svg",
"season": 2025,
"round": "Round of 16",
"standings": false
},
"teams": {
"home": {
"id": 25814,
"name": "Galícia U20",
"logo": "https://media.api-sports.io/football/teams/25814.pn
+g",
"winner": false
},
"away": {
"id": 13017,
"name": "Jacuipense U20",
"logo": "https://media.api-sports.io/football/teams/13017.pn
+g",
"winner": true
}
},
"goals": {
"home": 1,
"away": 2
},
"score": {
"halftime": {
"home": 1,
"away": 2
},
"fulltime": {
"home": null,
"away": null
},
"extratime": {
"home": null,
"away": null
},
"penalty": {
"home": null,
"away": null
}
},
"events": []
}
]
}
I removed use strict; because i was getting this error
Bareword "name" not allowed while "strict subs" in use at rss.pl line
+26.
Bareword "name" not allowed while "strict subs" in use at rss.pl line
+27.
When i try to run my code this is the error i get Not an ARRAY reference at script.pl line 18.
my full script is
#!/usr/bin/perl
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use JSON;
my $url = "https://v3.football.api-sports.io/fixtures?live=all";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $url);
$req->header('x-rapidapi-host' => 'v3.football.api-sports.io');
$req->header('x-rapidapi-key' => '.......');
my $response = $ua->request($req);
my $parse_json = JSON::XS->new->decode ($response->content);
my @matches = $parse_json->[1]; # Get all results in "response": [ ]
if ($response->is_success) {
foreach my $results (@matches) {
my $matche_status = $results->{status}->{short};
my $teamA = $results->{teams}->{home}>{name};
my $teamB = $results->{teams}->{away}>{name};
my $teamA_scores_HT = $results->{score}->{halftime}->{home};
my $teamB_scores_HT = $results->{score}->{halftime}->{away};
my $teamA_scores_FT = $results->{score}->{fulltime}->{home};
my $teamB_scores_FT = $results->{score}->{fulltime}->{away};
print "$matche_status | $teamA has scored $teamA_scores_HT in
+Halftime | $teamA_scores_FT in fulltime"; # Get all Home teams result
+s
print "$matche_status | $teamB has scored $teamB_scores_HT in
+Halftime | $teamB_scores_FT in fulltime"; # Get all Away teams result
+s
# Print Such
# HT | Arsenal has scored 1 in halftime | 2 in fulltime
# FT | Chelsea has scored 2 in halftime | 3 in fulltime
}
} else {
print $response->status_line;
}
|
I have a problem with my script. i cant output the response very well
this is the error i get
away_teamsARRAY(0x55fbf77894a0)timesARRAY(0x55fbf743be78)home_teamsARRAY(0x55fbf71ab7b0)scoresARRAY(0x55fbf71aba98)
this is the output i expect
times home_teams scores away_teams
12.05. 17:00 Brighton 1 - 4 Manchester City
Full code
#!/usr/bin/perl - wT
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTML::TreeBuilder;
use Data::Dumper;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); # show errors in
+browser
print "Content-type: text/html\n\n"; # print output to browser
my $url = 'https://www.flashscore.com/football/england/premier-league-
+2018-2019/results/';
my $ua = LWP::UserAgent->new;
sub get_scores {
my $response = $ua->get($url);
if ($response->is_success) {
my $tree = HTML::TreeBuilder->new;
$tree->parse($response->decoded_content);
return $tree;
} else {
die $response->status_line;
}
}
my $results = get_scores();
my @times = map { $_->as_text } $results->look_down(_tag => 'div', cla
+ss => 'event__time');
my @home_teams = map { $_->as_text } $results->look_down(_tag => 'div'
+, class => 'event__participant event__participant--home');
my @scores = map { $_->as_text } $results->look_down(_tag => 'div', cl
+ass => 'event__scores fontBold');
my @away_teams = map { $_->as_text } $results->look_down(_tag => 'div'
+, class => 'event__participant event__participant--away');
my %dict_res;
for my $ind ($results) {
push @{$dict_res{'times'}}, @times[$ind];
push @{$dict_res{'home_teams'}}, @home_teams[$ind];
push @{$dict_res{'scores'}}, @scores[$ind];
push @{$dict_res{'away_teams'}}, @away_teams[$ind];
print %dict_res;
}
|
I need to automate some tasks and this involves reading some MS Word files.
I recently came across MsOffice::Word::Surgeon.
Specifically what I want to do is read a word file, recognize tables in it and read tables, where specific columns may have relevant information, such as a key. Does anyone have or can give an example of this?
E.g.
use MsOffice::Word::Surgeon;
my $surgeon = MsOffice::Word::Surgeon->new(docx => $file);
my $main_text = $surgeon->document->plain_text;
# if I serialize this way, I lose table info
|
When I installed CPAN::SQLite and set cpan opt use_sqlite to true, the next time I started cpan shell I got this error:
2025-07-08 12:40:23 (3.53 MB/s) - ‘/home/somian/.cpan/sources/modules/
+03modlist.data.gz.tmp11438’ saved [248/248]
Database was generated on Mon, 07 Jul 2025 15:41:26 GMT
Updating database file ... DBD::SQLite::db commit failed: database is
+locked at /usr/local/lib/perl5/site_perl/CPAN/SQLite/Populate.pm line
+ 663, <DATA> line 268361.
DBD::SQLite::db commit failed: database is locked at /usr/local/lib/pe
+rl5/site_perl/CPAN/SQLite/Populate.pm line 663, <DATA> line 268361.
Catching error: "system /usr/local/bin/perl -MCPAN::SQLite::META=setup
+,update,check -e update failed: 2816 at /usr/local/lib/perl5/site_per
+l/CPAN/SQLite/META.pm line 318.\cJ" at /usr/local/lib/perl5/5.40.1/CP
+AN.pm line 397.
CPAN::shell() called at -e line 1
EDIT
Oh, s**t. I had an earlier instance of CPAN's shell running in the background in a terminal. I'm writing this update very quickly so I can say NEVER MIND, before anyone decides to waste their time telling me what's wrong. ;-).
This is on Gnu/Linux, perl 5.40.1, CPAN.pm version 2.36. The error is repeatable; if I set use_sqlite via o conf, I will get the error.
I understand what a locked database is, although I haven't had to learn the
specifics of database management (I've long been an "enthusiast" - self-taught -
rather than a "professional" who would be required to know databases for w$rk). My
questions are: has anyone else seen this error with CPAN?
And, What's the magnitude of the performance penalty for not
using SQLite with CPAN?
Soren
Jul 08, 2025 at 18:11 UTC
|
Hi,
I am trying to pick files and directories from multiple directories and folders for further processing in my programme logic.
I am able to pick majority of the files and directories. However, with -f and all other remaining file operators (that I have already tried so far), I am unable to pick following type of files from the cache folder.
1: /(a directory)/4-http???wscont1.apps.microsoft.co?winstore?1.8x?2d60181a-b6d7-499a-9414-f07b0f932419?AppTile.1.396657.404061.png
2: /(a directory)/4-https???wscont.apps.microsoft.com?winstore?6.3.0.1?100?GB?en-us?MS?467?features1de1406b-b4ec-4d86-9066-68bf9c5d67f2.json
3: (another file path)etc.
Can someone advise, which code to use to pick this type of files being present in 'a directory'?
I am using following code:-
```
for (@files) {
$element = $_;
if (-d $_) {
push @directories, process_files ($_);
++$dir_counter;
} elsif (-f $_) {
++$file_counter;
next;
} elsif ($_ =~ /\.dat$/i || $_ =~ /\.png$/i) {
++$file_counter;
next;
} else {
print NOW "I am in else statement: process_files($_)\n";
} # else loop end
```
|
I frequently use Python's matplotlib to make figures. I've made my own perl module for making quick plots in perl using matplotlib. I've found matplotlib to make figures inconsistently, and requires a lot more time for me to switch between the languages. So my perl scripts make a python temp file, and execute it. It's not super-efficient, I know, but it saves me time in both writing scripts and reading documentation.
Plotting a hash is very easy:
use Matplotlib::Simple 'plot';
plot({
'output.filename' => 'svg/single.barplot.svg',
data => { # simple hash
Fri => 76, Mon => 73, Sat => 26, Sun => 11, Thu => 94, T
+ue => 93, Wed => 77
},
'plot.type' => 'bar',
xlabel => '# of Days',
ylabel => 'Count (Applications)',
title => 'Rejections by Days',
});
My question: Should I put this module on CPAN?
|