How to display the value of a Perl variable in a tooltip - perl

I've got a CGI perl script which processes a large Application Configuration file and displays extracted config data in table format.
One of the columns shows the hostname for a given server extracted from the config file.
What I'd like to do is have the alias name displayed (stored in $alias) when the user hovers over the hostname - much like a tooltip.
Each table row will have a different hostname and alias and I'm creating the table by iterating through an array.
I'm using the object-oriented approach to CGI Perl.
I tried the using the following:
print $cgi->start_td({class=>'primpeer',title=>'$aliasName'}),"$hostName";
but this just echoed the $aliasName in the tooltip rather than the contents of $aliasName

Put the value of your $alias into a title attribute (presumably of a td element). It will produce a tooltip for you like the alt attribute of an img element.

Variables inside single quote are not interpolled.
Just use double quote instead of single quote:
print $cgi->start_td({class=>'primpeer',title=>"$aliasName"}),"$hostName";
here __^ here __^
or without any quotes:
print $cgi->start_td({class=>'primpeer', title=>$aliasName}), $hostName;

Related

Using Perl to parse text from blocks

I have a file with multiple blocks of test. FOR EACH block of test, I want to be able to extract what is in the square bracket, the line containing the FIRST instance of the word "area", and what is on the right of the square bracket. Everything will be a string. Essentially what I want to do is store each string into a variable in a hash so i can print it into a 3 column csv file.
Here's a sample of what the file looks like:
Student-[K-6] Exceptional in Math
/home/area/kinder/mathadvance.txt, 12
Students in grade K-12 shown to be exceptional in math.
Placed into special after school program.
See /home/area/overall/performance.txt, 200
Student-[Junior] Weak Performance
Students with overall weak performance.
Summer program services offered as shown in
"/home/area/services/summer.txt", 212
Student-[K-6] Physical Excerise Time Slots
/home/area/pe/schedule.txt, 303
Assigned time slots for PE based on student's grade level. Make reference to
/home/area/overall/classtimes.txt, 90
I want to to have a final csv file that looks like:
Grade,Topic,Path
K-6, Exceptional in Math, /home/area/kinder/mathadvance.txt, 12
K-6, Physical Exercise Time Slots, /home/area/pe/schedule.txt, 303
Junior, Weak Performance, "/home/area/services/summer.txt", 212
Since it's a csv file, I know it will also separate at the line number when exporting into excel but I'm fine with that.
I started off by putting the grade type into an array because I want to be able to add more strings to it for different grade levels.
My program looks like this so far:
#!/usr/bin/perl
use strict;
use warnings;
my #grades = ("K-6", "Junior", "Community-College", "PreK");
I was thinking that I will need to do some sort of system sed command to grab what is in the brackets and store it into a variable. Then I will grab everything to the right of the bracket on the line and store it into a variable. And then I will grep for a line containing "area" to get the path and I will store it as a string into a variable, put these in a hash, and then print into csv. I'm not sure if I'm thinking about this the right way. Also, I have NO IDEA how to do this for each BLOCK of text in the file. I need it by block because each block has its own corresponding grades, topics, and paths.
perl -000 -ne '($grade, $topic) = /\[(.*)\] (.*)/;
($path) = m{(.*/area/.*)};
print "$grade, $topic, $path\n"' -- file.txt
-000 turns on paragraph mode, -n won't read line by line, but paragraph by paragraph
/\[(.*)\] (.*)/ matches the square brackets and whatever follows them up to a newline. The inside of the square brackets and the following text are captured using the parentheses.
m{(.*/area/.*)} captures the line containing "area". It uses the m{} syntax instead of // so we don't have to backslash the slashes (avoiding so called "leaning toothpick syndrome")

Sed command inside TCL script

Help me understand the syntax sed.I removed single quotes, but the code still does not work.
set id [open file.txt]
# send the request, get a lot of data
set tok [::http::geturl "http://example.com"-channel $id]
# cut out the necessary data between two words
exec sed s/{"data1":\(.*\)/data2\1/ $id
close $id
set ir [open file.txt]
set phone [read $ir]
close $ir
puts $phone
The problem is that I get data from a query of the following kind
{"id":3876,"form":"index","time":21,"data":"2529423","service":"Atere","response":"WAIT"}
The brace is an element of the syntax of the language, and I need to cut exactly the value between the word and the brace. How to implement this in a script.
Your code is rather confused, as (a) you are passing a file handle to the sed command. That's not going to work. (b) you are passing an input channel to http rather than an output channel (try opening the file for writing).
About the underlying problem.
If you are receiving basic JSON data back as shown.
a) You can use a JSON parser: tcllib's json module
b) Convert it to a form that Tcl can parse as a dictionary
# Assuming the JSON data is in the $data variable, and there's no
# other data present. This also assumes the data is very basic
# there are no embedded commas. Many assumptions means this
# code is likely to break in the future. A JSON parser would
# be a better choice.
set data "\{"
append data {"id":3876,"form":"index","time":21,"data":"2529423","service":"Atere","response":"WAIT"}
append data "\}"
regsub -all {[{}:",]} $data { } data
set mydatadict $data
puts [dict get $mydatadict id]
Edit:
For http processing:
set tok [::http::geturl "http://example.com"]
set data [::http::data $tok]
::http::cleanup $tok

Perl CGI - How can I delete contents of text fields?

So, I am totally new with CGI programming in Perl.
The question is simple. Is there any chance to delete the content of a text field in CGI?
I must to write a code that have some popup_menu, submit button and text fields (area).
When I click on the submit button the program reads the value from one of the popup_menu.
The task is to copy this content into text field and then when I choose another element from the popup_menu (and click on the submit button of course), let the new content write into the text field replace the old one.
I think perldoc.perl.org gives only a little information about CGI programming. I'd have lot of questions in thema... :(
Any help would be approciate!
I guess, what you describe is: when you click the submit button, then your cgi script will run, given the parameters you entered in the form. What I then has to do is: write something back and print the form again - with different values.
So even if this is not the perfect way of doing such kind of things (for simple form element substitution you should do it client side and use javascript - you don't need a cgi backend script for this), let's see how a cgi script might look like.
First it's important to know, how you write your form. Let's assume you write it "the hard way" with print.
What your script has to do is parse the input and then add it as a value to the output.
use CGI;
my $q = CGI->new;
# get the value from the popup / html select
my $popup_value = $q->param('popup_menu'); # name of the <select name="..."> in your html
# ...
# writing the form
print $q->header;
# some more prints with form etc.
print textarea( -name => 'text_area',
-default => $popup_value // '', # will use empty string on first call
);
# Don't turn off autoescaping !
BTW, the value of a select option is meant to be a short indicator, not a full text (even this might be possible up to a certain amount of characters). So you might think of building a hash or an array with the appropriate values to be printed in the text area and give your select options the values 0, 1, 2 ...
my #text_values = ('', 'First text', 'second text', 'third text');
my $popup_value = $q->param('popup_menu') || 0; # default index.
# now use 1,2,3, ... as values in your popup_menu options
# ...
print textarea( -name => 'text_area',
-default => $text_values[$popup_value] );

Automating Lotus Notes Text Style with perl?

I'm trying to automate the sending of an email with an embedded attachment and some text that implements HTML code but it seems that the code that I am using will not allow me to create an HTML bolded text or a unordered list. To double check I created the code in vba then passed it through a vba to perl converter and it matched up with what I had written. Here is the part of my script that handles creating the text and embedded attachment in the email:
my $richStyle = $Document->NotesRichTextStyle();
$richStyle->{'PassThruHTML'} = 1;
my $Body = $Document->CreateRichTextItem('Body');
$Body->AppendText(">>EOT");
$Body->AppendStyle($richStyle);
**$Body->AppendText("<b>HELLO</b>");**
$Body->EmbedObject(EMBED_ATTACHMENT,'','$filename','$name');
I get this error:
Not a HASH reference at line $richStyle->{'PassThruHTML'} = 1;
The main point of this code was so that i could use HTML Tags inside the email
My best guess:
my $richStyle = $Document->NotesRichTextStyle();
From the designer help:
Set notesRichTextStyle = notesSession.CreateRichTextStyle( )
You need to create the notesRichTextStyle using the session.
I guess you want to create an HTML mail?
In that case, it would be better to use the MIME entity classes to generate native HTML mails and not to rely on the NotesRichText to HTML conversion.
You can find more info on the MIME entity in the Designer Help: http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=/com.ibm.designer.domino.main.doc/H_NOTESMIMEENTITY_CLASS_OVERVIEW.html
$ perl -Mdiagnostics -e " []->{1}=2 "
Not a HASH reference at -e line 1 (#1)
(F) Perl was trying to evaluate a reference to a hash value, but found a
reference to something else instead. You can use the ref() function to
find out what kind of ref it really was. See perlref.
Uncaught exception from user code:
Not a HASH reference at -e line 1.
at -e line 1
so whatever NotesRichTextStyle returns doesn't like ->{...} so maybe use ->SetProperty... or something else :/

Print freeform text in a Perl function?

I have been getting a very odd error when trying to print freeform text in a subroutine in Perl. Below is the code I am calling
print OUTFILE <<"HEADER";
The freeform text would go here
HEADER
The odd thing is that this only works in the main of my function. As soon as I place it in a function call, I get this error:
Can't find string terminator "HEADER" anywhere before EOF
Meaning it can't find the HEADER, even though it is there. Can you not use freeform text within a function (subroutine)?
Make sure that there is no space/tab/indentation before ending string identifier, that is HEADER. Your code should look like this:
function someFunc(){
print OUTFILE <<"HEADER";
The freeform text would go here
HEADER
}
Notice that there is no space/tab/indentation before HEADER there. It should start from first character of its line.
Check this tutorial out for more information:
Perl Here-Doc Tutorial
Quoting:
The important rule to remember is that
you finish a here-doc using the same
word you started, and it must be by
itself on the line