Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Your first Perl script

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#21 Post by dasein »

GarryRicketson wrote:I need to look at some of the tutorial on "loops" now...
Yes you do. ;)
GarryRicketson wrote:At this point I would just like it to repeat, and give another problem, keep repeating and just use ctr-l c to exit, when I am ready.
Can you think of a more kid-friendly way to end the program?
GarryRicketson wrote:
it's called the off-by-one (OBO) bug.
I suppose I should try doing a search on that as well.
Edit I see you found it. But you've only fixed half of it. :mrgreen:

Garry, it seems clear that you'd like more help with this, but figuring stuff out for yourself has at least two advantages:

1) We all learn better when we can apply what we've learned to a project we're actively working on. This is why "write a 'russian roulette' program" is a mainstay of programming 101 homework.

2) We learn other stuff along the way. That stuff (called "incidental learning") is a vital part of the larger learning experience.

The vast majority of programming involves no more than a handful of basic operations and concepts. Focus on learning those concepts, rather than becoming mired in syntax. This "concept-centered approach" will help you see where what you're learning about in perl maps to something you already know from BASIC. It'll take some time and some practice, but you seem to be having a pretty good time, so enjoy the ride.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#22 Post by GarryRicketson »

Thanks, yes I am having fun with this, every time I work on it , and look at some of the tutorials, I do gain a little, and that is encouraging.
Can you think of a more kid-friendly way to end the program?

Oh, I am sure there is, and there is a lot I need to think of and then figure ot
to make it more fun, the qbasic one had some sounds, and played some tunes using the
"play" command, and also I made a "piano" that one could play tunes with, just using the "beep", and play command....there will be more , but like you are saying I need to get more familiar and comfortable with perl and just some basic operations and concepts. with it.
On the "ctrl-c", that is what I first learned, on dos, but also this may sound surprising,
the "ctrl-c" is something she all ready is familiar with, and she is only 4 yrs old, she still does not know any addition, counting, nor reading really... so I have plenty of time on this.
I did find, and have grasped some concepts for some text stuff,that are really easy, and can help her start learning letters and spelling.
Part of it is also, but she is still a little to young, but the idea is to give her something she can edit, like a template,... for example, once she knows the times table 1 through 10, she can also change the values her self, and start getting numbers above 10,..
Similar she herself can change the text stuff , to say things the want she wants,..etc.
But any way, now I am getting ahead of myself, a little bit at a time,.
thanks,...

User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: My first Perl script

#23 Post by golinux »

GarryRicketson wrote:
by golinux »It's a perfect diversion for when you need a break and a good laugh!
I wish I could hear them, or it was "closed" captchined, I felt like I am missing the humor since I could not hear what they say.
I hope that comment didn't seem insensitive. If it's any consolation, the talk was captioned in several screens during the event. Did you notice that? You could always write to them and perhaps request they do split screen captioning on videos in the future.
May the FORK be with you!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#24 Post by GarryRicketson »

I hope that comment didn't seem insensitive
No not at all , and the parts that they did show some "captions" and examples, I did read, so I am not sure how much or what I missed in not hearing them.(if anything) Any way thanks for sharing it, I can look at it again and pay more attention to the parts with captions, like you say, it is a good diversion when I, or anyone wants to take a break, for a laugh,..
thanks
I am the sort of person that even reading a joke, or piece of humor, it usually takes me a few times , before I "get it", and see the humor.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#25 Post by dasein »

GarryRicketson wrote:...she is only 4 yrs old, she still does not know any addition, counting, nor reading really... so I have plenty of time on this.
Enough time so that you will have forgotten more then you've learned so far. (Programming is not like riding a bicycle; unused skills and concepts definitely atrophy with time.)

That said, I'm actually a huge fan of early exposure to arithmetic and even very rudimentary math at a very young age, particularly when that exposure is framed in terms of math as a different "language." (And so it surely is; mathematics is the language of Nature.)

Human children have an amazing ability to absorb languages up to about age 7. Expose them to one, they learn one. Expose them to three, they learn three. The ability seems to be hard-wired into our brains. And at its root, math obeys exactly the same principles as any other language. (That is to say, rules regarding, say, subject-verb agreement and rules regarding, say, properties of multiplication are not so much different as they are merely distinct.)

Sorry. End of pedagogical rant.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#26 Post by GarryRicketson »

OK, well thanks again Dasein, and then this, I had not yet thought about what to say really, but you are right, :
Garry, it seems clear that you'd like more help with this, but figuring stuff out for yourself has at least two advantages:
1) We all learn better when we can apply what we've learned to a project we're actively working on. This is why "write a 'russian roulette' program" is a mainstay of programming 101 homework.
2) We learn other stuff along the way. That stuff (called "incidental learning") is a vital part of the larger learning experience.
The vast majority of programming involves no more than a handful of basic operations and concepts. Focus on learning those concepts, rather than becoming mired in syntax. This "concept-centered approach" will help you see where what you're learning about in perl maps to something you already know from BASIC. It'll take some time and some practice, but you seem to be having a pretty good time, so enjoy the ride.
====================================================================
Ok that is well said, and I agree, like wise yes, I like some help, your pointing out the "off by one" "bug", for example, was a big help and just about the same time I was going to "google" that, to get more clues, I remembered in some qbasic tutorial, that was one of the first things they mentioned.
Just to clarify, the bigger motive in starting this topic/thread, not so much that I need help, the material and tutorials are on the "Web", all the help I need is out there, but I enjoying sharing what I do with my Debian system, even though Perl scripts, and any programming can be done on any OS, that part is not to important, but it can be a factor, what works on one OS/distro, might not work on another.
Any way, I posted my first attempt at perl, but in no way did I intend the topic/thread to be limited to just "Garry's first Perl script", my intention was to try to start something for others, if there are any on this forum trying to start learning how to wtite some perl scripts, to perform what ever tasks, or perhaps even a game or something.
I enjoy seeing what others are trying to do as well, ideas they have ,etc. Of course
if people think "Perl is dead", well those would not have much interest, and they can all ways start a topic on whatever programming language interests them as well.
There are so many very good tutorials, even with good search skills it can be kind of overwhelming, and one can waste a lot of time going to sites, and looking at a tutorial, browsing , trying to find parts that relate to what they do not understand, etc.
So anyway, after looking several I finally stumbled on to this one: I like it, so I downloaded some parts.

http://perl-begin.org/tutorials/perl-for-newbies/

With all that said, anyone else is more then welcome to share their "first scripts", or even more advanced "projects" if they want to.

I do want to add though, anyone that does not understand what any script will actually do, or how it will work should not simply copy/paste it into a file and make it executable, perl is powerfull enough , in the wrong hands a system could be "borked" very easily, or comprimised.
OK well I guess that is about it for now. Thanks for your interest, both Dasein and GOLINUX, I am happy at least a couple of forum members showed some interest.
The topic is pretty new, I just wanted to make it clear it was not intended as only looking for help for my script, but also intended for others as well. In fact I wanted to also demonstrate how even though I knew nothing about perl, using tutorials and some experimenting I was able to come up with a "crude , rude" but working "mini" program.
With just a little help, like "clues" etc,.. it makes it more fun when others show interest and are willing to take the time to respond. Thanks.
P.S. Eventually, when I have this more better( "chido",we say in Mexico) I mean, when my little program is more "cool", I will share the finished program.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Your first Perl script

#27 Post by GarryRicketson »

I like this better:,
This is how it looks when running it:

Code: Select all

perl-tests$ ./aamath.pl
Type in the correct answer, 
Hit enter key to continue, if it is not correct
 the program will not move on to a new problem, 
 think about your answer, and try again, when it is
 the correct answer, it will tell you, 
 and give a new problem.
 6 x 0 = 0
Great 0 is correct.
hit enter to continue, ctr-c to exit.
4 x 4 = 16
Great 16 is correct.
hit enter to continue, ctr-c to exit.
3 x 1 = 5
9
6
4
3
Great 3 is correct.
hit enter to continue, ctr-c to exit.
The screen clears , after each problem is correctly solved, and a new one displayed.

Code: Select all

#!/usr/bin/perl
print "Type in the correct answer, \n";
print "Hit enter key to continue, if it is not correct\n ";
print "the program will not move on to a new problem, \n ";
print "think about your answer, and try again, when it is\n ";
print "the correct answer, it will tell you, \n ";
print "and give a new problem.\n ";
for( ; ; )
{
my $random = int rand 11;
my $random2 = int rand 11;
print "$random x $random2 = ";
$x = $random * $random2;
while ( $answer ne "$x" ) {
	$answer = <STDIN>; 
chomp $answer;
;
}
if ( $answer eq "$x" ) {
}
print "Great $answer is correct.\n";
print "hit enter to continue, ctr-c to exit.";
$_ = <STDIN>;
print "\033[2J";
#print "\033[2J"Might not work on some systems but it does on mine
}  
One thing I like about Perl, and a little tip, I did not see in any of the tutorials,
If you have perl installed, working form the command line,
Type:

Code: Select all

perl 

then enter: (for example)

Code: Select all

 /perl-tests$ perl
print "Type in the correct answer, \n";
[code]
Then use "ctrl-d" to execute the line:
on this it then displays 
[quote]Type in the correct answer, [/quote]
But if it is not a valid command ,or string, it tries to give hints, as to why. 
[code]perl-tests$ perl
but if the command is wrong
This is what it says, when I hit <ctrl-d>
Can't locate object method "is" via package "wrong" (perhaps you forgot to load "wrong"?) at - line 1.

Perl is a fun, easy programming language to learn, I am really enjoying this.
Last edited by GarryRicketson on 2016-06-30 03:53, edited 1 time in total.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Your first Perl script

#28 Post by GarryRicketson »

Now in Spanish, and some better methods.

Code: Select all

#!/usr/bin/perl
#use strict;
#use warnings;

print "Por favor escribe tu nuombre:\n";
$name = <>;
chomp($name);
print "Hola!, ", $name, "!\n";
print " $name, Vamos a hacer tablas ,tecla algunas letras o numeros:\n";
$string = <>;
chomp($string);
print "Lo que teclado,tiene:\n",length($string),"  letras o numeros\n";
$_= <STDIN>;
print "[Instrucciones, ctrl-c o \"quit\" para salir]\n";
print "Entra para continuar\n";
$_ = <STDIN>;
OUTER: {
    my ($x, $y) = map { int rand $_ } qw/11 11/;
    INNER: {
        print "$x x $y = ";
        local $_ = <STDIN>;
        chomp;
        last OUTER if m/^quit$/;
        redo INNER unless m/^\d+$/;
        if ($_ == ($x*$y))
  { print "Muy buen es correcto!\n"; redo OUTER }
        else
          { print "Oops!No es correcto,haga otra vez\n"; redo INNER }

    }
}
 

oldtechaa
Posts: 52
Joined: 2016-02-10 14:27

Re: Your first Perl script

#29 Post by oldtechaa »

My first Perl script was written a few days ago, and played some helicopter and organ sounds into a MIDI file using libmidi-perl, which is originally a http://www.cpan.org Perl module. I highly recommend using CPAN for modules to use in larger projects. Perl is a very nice easy language. I might post my script sometime.

I am now planning a graphical MIDI sequencer using Tk from CPAN.(libtkx-perl in Debian)

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Your first Perl script

#30 Post by GarryRicketson »

I might post my script sometime.
I will look forward to that, (or "we" but I can not speak for others).
and played some helicopter and organ sounds into a MIDI file using libmidi-perl,
That sounds interesting, I am thinking as well, to try to add some sounds, and maybe even graphics, I have the CPAN modules, but have not yet tried anything using them.
thank you for sharing.

oldtechaa
Posts: 52
Joined: 2016-02-10 14:27

Re: Your first Perl script

#31 Post by oldtechaa »

This is the original script. I'm sorry you may not be able to hear the output of sound-related scripts, but this one isn't very pleasant anyway.

Code: Select all

use strict;
use warnings;
use MIDI;

#foreach (@MIDI::Event::All_events) {
#	print "$_\n";
#}

my $organ_track = MIDI::Track->new();
$organ_track->events(
	['patch_change',    0, 0,  19],
	['patch_change',    0, 1, 124],
	[     'note_on',    0, 0,  50, 64],
	[     'note_on',    0, 1,  50, 96],
	[    'note_off',   96, 0,  50, 64],
	[     'note_on',   96, 0,  49, 64],
	[    'note_off',   96, 1,  50, 96],
	[     'note_on',   96, 1,  49, 90],
	[    'note_off',  192, 0,  48, 64],
	[     'note_on',  192, 0,  49, 64],
	[    'note_off',  192, 1,  48, 90],
	[     'note_on',  192, 1,  49, 90],
	[    'note_off',  288, 0,  48, 64],
	[     'note_on',  288, 0,  49, 64],
	[    'note_off',  288, 1,  48, 90],
	[     'note_on',  288, 1,  49, 90],
	[    'note_off',  384, 0,  48, 64],
	[     'note_on',  384, 0,  49, 64],
	[    'note_off',  384, 1,  48, 90],
	[     'note_on',  384, 1,  49, 90],
	[    'note_off',  480, 0,  48, 64],
	[     'note_on',  480, 0,  49, 64],
	[    'note_off',  480, 1,  48, 90],
	[     'note_on',  480, 1,  49, 90],
	[    'note_off',  576, 0,  48, 64],
	[     'note_on',  576, 0,  49, 64],
	[    'note_off',  576, 1,  48, 90],
	[     'note_on',  576, 1,  49, 90],
	[    'note_off',  672, 0,  48, 64],
	[     'note_on',  672, 0,  49, 64],
	[    'note_off',  672, 1,  48, 90],
	[     'note_on',  672, 1,  49, 90],
	[    'note_off',  768, 0,  48, 64],
	[     'note_on',  768, 0,  49, 64],
	[    'note_off',  768, 1,  48, 90],
	[     'note_on',  768, 1,  49, 90],
	[    'note_off',  864, 0,  48, 64],
	[     'note_on',  864, 0,  49, 64],
	[    'note_off',  864, 1,  48, 90],
	[     'note_on',  864, 1,  49, 90],
	[    'note_off',  960, 0,  48, 64],
	[     'note_on',  960, 0,  49, 64],
	[    'note_off',  960, 1,  48, 90],
	[     'note_on',  960, 1,  49, 90],
	[    'note_off', 1056, 0,  48, 64],
	[    'note_off', 1056, 1,  48, 90]
);

my $opus = MIDI::Opus->new();
$opus->format(0);
$opus->ticks(1056);
$opus->tracks($organ_track);

# {'format' => 0, 'ticks' => 100, 'tracks' => [$organ_track]});

$opus->write_to_file("test.mid");
And this is the start of my MIDI sequencer software; pretty much the same thing, just made nicer. (This code is released under the Perl license. Just had to throw that in there for if/when I release it.)

Code: Select all

#!/usr/bin/perl

# I don't know if this is a legally binding copyright, but
# Copyright 2016 oldtechaa (Easily found on the Internet.)
# This software is redistributable as long as it is not modified for inclusion or copied verbatim into non-free software as defined by the FSF and DFSG.

use strict;
use warnings;

use MIDI;

# writes the MIDI output to a file based on the list of events and the filename
sub midiWrite {
	my $midiEventsRef = shift;
	my $midiTicks = shift;
	my $midiFile = shift;
	
	my $midiTrack = MIDI::Track->new({'events' => $midiEventsRef});
	my $midiPiece = MIDI::Opus->new({'format' => 0, 'ticks' => $midiTicks, 'tracks' => [$midiTrack]});
	$midiPiece->write_to_file($midiFile);
};

my $events = [
	['patch_change',    0, 0,  19],
	['patch_change',    0, 1, 124],
	[     'note_on',    0, 0,  50, 64],
	[     'note_on',    0, 1,  50, 96],
	[    'note_off',   96, 0,  50, 64],
	[     'note_on',   96, 0,  49, 64],
	[    'note_off',   96, 1,  50, 96],
	[     'note_on',   96, 1,  49, 90],
	[    'note_off',  192, 0,  48, 64],
	[     'note_on',  192, 0,  49, 64],
	[    'note_off',  192, 1,  48, 90],
	[     'note_on',  192, 1,  49, 90],
	[    'note_off',  288, 0,  48, 64],
	[     'note_on',  288, 0,  49, 64],
	[    'note_off',  288, 1,  48, 90],
	[     'note_on',  288, 1,  49, 90],
	[    'note_off',  384, 0,  48, 64],
	[     'note_on',  384, 0,  49, 64],
	[    'note_off',  384, 1,  48, 90],
	[     'note_on',  384, 1,  49, 90],
	[    'note_off',  480, 0,  48, 64],
	[     'note_on',  480, 0,  49, 64],
	[    'note_off',  480, 1,  48, 90],
	[     'note_on',  480, 1,  49, 90],
	[    'note_off',  576, 0,  48, 64],
	[     'note_on',  576, 0,  49, 64],
	[    'note_off',  576, 1,  48, 90],
	[     'note_on',  576, 1,  49, 90],
	[    'note_off',  672, 0,  48, 64],
	[     'note_on',  672, 0,  49, 64],
	[    'note_off',  672, 1,  48, 90],
	[     'note_on',  672, 1,  49, 90],
	[    'note_off',  768, 0,  48, 64],
	[     'note_on',  768, 0,  49, 64],
	[    'note_off',  768, 1,  48, 90],
	[     'note_on',  768, 1,  49, 90],
	[    'note_off',  864, 0,  48, 64],
	[     'note_on',  864, 0,  49, 64],
	[    'note_off',  864, 1,  48, 90],
	[     'note_on',  864, 1,  49, 90],
	[    'note_off',  960, 0,  48, 64],
	[     'note_on',  960, 0,  49, 64],
	[    'note_off',  960, 1,  48, 90],
	[     'note_on',  960, 1,  49, 90],
	[    'note_off', 1056, 0,  48, 64],
	[    'note_off', 1056, 1,  48, 90]
];

midiWrite($events, 1056,"Milestone1Test.mid");
Both of these will require the installation of libmidi-perl.

Edit: I changed the license. I still have to decide on the eventual license for the software, so I changed this second script to redistributable for use in free software as defined by either the FSF or DFSG.
Check out SeekMIDI, a simple, lightweight MIDI sequencer at https://github.com/oldtechaa/seekmidi/!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Your first Perl script

#32 Post by GarryRicketson »

Thanks for sharing these, they seem to work ok, my kid confirmed , on the sound,
but he said it was "chido" (cool) .

oldtechaa
Posts: 52
Joined: 2016-02-10 14:27

Re: Your first Perl script

#33 Post by oldtechaa »

The link in my signature is the Git repository for my MIDI sequencer, if you ever want to keep up with it and see how my learning is progressing. :)
Check out SeekMIDI, a simple, lightweight MIDI sequencer at https://github.com/oldtechaa/seekmidi/!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Your first Perl script

#34 Post by GarryRicketson »

Ok, thanks, I had noticed that,thanks.

Post Reply